<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      <a-row>

	<a-col :span="12">
        <a-form-item label="应聘者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['candidateId', validatorRules.candidateId]" placeholder="请输入应聘者"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['departmentId']" placeholder="请输入部门"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['position', validatorRules.position]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =POSITION" placeholder="请选择职位"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="合同期（年）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['contractPeriod', validatorRules.contractPeriod]" placeholder="请输入合同期"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="合同附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['uploadPath']" :isMultiple="false" :trigger-change="true"></j-upload>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="试用期（月）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['probationPeriod', validatorRules.probationPeriod]" placeholder="请输入试用期"></a-input>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="员工类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['workingHours', validatorRules.workingHours]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = WORK_TYPE" placeholder="请选择员工类型"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="年薪" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['annualSalary', validatorRules.annualSalary]" placeholder="请输入年薪" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="月薪" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['monthlySalary', validatorRules.monthlySalary]" placeholder="请输入月薪" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="预期报到日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择预期报到日期" v-decorator="['arrivalTime', validatorRules.arrivalTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        </a-col>
	<a-col :span="12">
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['statusId', validatorRules.statusId]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = APPLY_STATUS" placeholder="请选择状态"/>
        </a-form-item>
        </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JUpload from '@/components/jeecg/JUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  export default {
    name: "HrOfferModal",
    components: { 
      JDate,
      JUpload,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          candidateId: {
            rules: [
              { required: true, message: '请输入应聘者!'},
            ]
          },
          position: {
            rules: [
              { required: true, message: '请输入职位!'},
            ]
          },
          contractPeriod: {
            rules: [
              { required: true, message: '请输入合同期!'},
            ]
          },
          probationPeriod: {
            rules: [
              { required: true, message: '请输入试用期!'},
            ]
          },
          workingHours: {
            rules: [
              { required: true, message: '请输入员工类型!'},
            ]
          },
          annualSalary: {
            rules: [
              { required: true, message: '请输入年薪!'},
            ]
          },
          monthlySalary: {
            rules: [
              { required: true, message: '请输入月薪!'},
            ]
          },
          arrivalTime: {
            rules: [
              { required: true, message: '请输入预期报到日期!'},
            ]
          },
          statusId: {
            rules: [
              { required: true, message: '请输入状态!'},
            ]
          },
        },
        url: {
          add: "/online/api/createHrOffer",
          edit: "/online/api/updateHrOffer",
          get: "/online/api/getHrOffer",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        if(record.id){
	      httpAction(this.url.get,{id:record.id},"post").then((res)=>{
	         if(res.success){
		        this.model = Object.assign({}, res.result);
		        this.visible = true;
		        this.$nextTick(() => {
		           this.form.setFieldsValue(pick(this.model,'candidateId','departmentId','position','contractPeriod','uploadPath','probationPeriod','workingHours','annualSalary','monthlySalary','arrivalTime','statusId'))
		        })   
	         }
	      })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success('提交成功!');
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'candidateId','departmentId','position','contractPeriod','uploadPath','probationPeriod','workingHours','annualSalary','monthlySalary','arrivalTime','statusId'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>
